package com.jddup.program.comparator.sqlserver;

import com.jddup.common.pojo.TableColumn;
import com.jddup.common.pojo.TableMeta;
import com.jddup.program.comparator.CreateDDLBuilder;
import com.jddup.program.comparator.mysql.MysqlColumnSqlBuilder;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * Created by zhangzhenbin on 17-12-11.
 */
public class SqlServerCreateDDLBuilderImpl implements SqlServerColumnSqlBuilder, CreateDDLBuilder {
    private static final String CREATE_TABLE_SQL =
            "BEGIN\r\n" +
                    "   CREATE TABLE [{tableName}] ( {columns} )\r\n" +
                    "${tableDescriptionSql}" +
                    "${columnsDescriptionSql}" +
                    "END";


    public String tableDescriptionSql(TableMeta tableMeta) {
        if (StringUtils.isNotBlank(tableMeta.getTableComment())) {
            return "   EXEC sys.sp_addextendedproperty @name=N'MS_Description',@level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE', @level1name=N'" +
                    tableMeta.getTableName() + "', @value=N'" + tableMeta.getTableComment().
                    replace("'", "").
                    replace("\"", "").
                    replace(" ", "").
                    replace("\n", "") + "'\r\n";
        } else return "";
    }

    public String columnDescriptionSql(String tableName, TableColumn tableColumn) {
        if (StringUtils.isNotBlank(tableColumn.getComment())) {
            return "   EXEC sys.sp_addextendedproperty @name=N'MS_Description',@level0type=N'SCHEMA',@level0name=N'dbo', " +
                    "@level1type=N'TABLE', @level1name=N'" + tableName + "',@level2type=N'Column',@level2name=N'" + tableColumn.getColumnName() + "', " +
                    "@value=N'" + tableColumn.getComment().
                    replace("'", "").
                    replace("\"", "").
                    replace(" ", "").
                    replace("\n", "") + "'\r\n";
        } else return "";
    }

    @Override
    public String build(TableMeta tableMeta) {
        List<TableColumn> tableColumns = tableMeta.getTableColumns();
        StringBuilder columnDDL = new StringBuilder();
        String tableDescriptionSql = tableDescriptionSql(tableMeta);
        StringBuilder columnsDescriptionSql = new StringBuilder();
        for (TableColumn tableColumn : tableColumns) {
            buildColumnSql(tableColumn, columnDDL);
            columnDDL.append(",");
            columnsDescriptionSql.append(columnDescriptionSql(tableMeta.getTableName(), tableColumn));
        }
        if (columnDDL.length() > 0) {
            columnDDL.deleteCharAt(columnDDL.length() - 1);
        }
        return getTemplateSQL()
                .replace("{tableName}", tableMeta.getTableName())
                .replace("{columns}", columnDDL)
                .replace("{tableDescriptionSql}", tableDescriptionSql)
                .replace("{columnsDescriptionSql}", columnsDescriptionSql.toString())
                .replace("{tableComment}", tableMeta.getTableComment().
                        replace("'", "").
                        replace("\"", "").
                        replace(" ", "").
                        replace("\n", ""));
    }

    @Override
    public String getTemplateSQL() {
        return CREATE_TABLE_SQL;
    }


    @Override
    public String getQuote() {
        return "\"";
    }
}
